home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Text / Edit / GoldED-Demo / installdata / golded / developer / api / examples / button / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  5.0 KB  |  200 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "defs.h"
  15.  
  16. Prototype LibCall struct Library *LibInit   (__D0 BPTR);
  17. Prototype LibCall struct Library *LibOpen   (__D0 long, __A0 struct Library *);
  18. Prototype LibCall long            LibClose  (__A0 struct Library *);
  19. Prototype LibCall long            LibExpunge(__A0 struct Library *);
  20.  
  21. struct ExecBase *SysBase       = NULL;
  22. struct Library  *DOSBase       = NULL;
  23. struct Library  *GfxBase       = NULL;
  24. struct Library  *IntuitionBase = NULL;
  25. struct Library  *LibBase       = NULL;
  26. struct Library  *GadToolsBase  = NULL;
  27.  
  28. BPTR SegList = 0;
  29.  
  30. /*
  31.  *    The Initialization routine is given only a seglist pointer.  Since
  32.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  33.  *    and return either NULL or the library pointer.  Exec has Forbid()
  34.  *    for us during the call.
  35.  *
  36.  *    We use an extended library structure to allow identification as an
  37.  *    API client
  38.  */
  39.  
  40. LibCall struct Library *
  41. LibInit(__D0 BPTR segment)
  42. {
  43.     struct Library *lib;
  44.  
  45.     static const long Vectors[] = {
  46.  
  47.         (long)ALibOpen,
  48.         (long)ALibClose,
  49.         (long)ALibExpunge,
  50.         (long)ALibReserved,
  51.         (long)APIMountClient,
  52.         (long)APICloseClient,
  53.         (long)APIBriefClient,
  54.         (long)APIFree,
  55.         -1
  56.     };
  57.  
  58.     SysBase = (struct ExecBase *)*(long *)4;
  59.  
  60.     if (DOSBase = OpenLibrary("dos.library", 0)) {
  61.  
  62.         if (GfxBase = OpenLibrary("graphics.library", 0)) {
  63.  
  64.             if (IntuitionBase = OpenLibrary("intuition.library", 0)) {
  65.  
  66.                 if (GadToolsBase = OpenLibrary("gadtools.library", 0)) {
  67.  
  68.                     if (LibBase = lib = MakeLibrary((APTR)Vectors, NULL, NULL, sizeof(struct APIBase), (BPTR)NULL)) {
  69.  
  70.                         lib->lib_Node.ln_Type = NT_LIBRARY;
  71.                         lib->lib_Node.ln_Name = LibName;
  72.                         lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  73.                         lib->lib_Version      = 38;
  74.                         lib->lib_Revision     = 0;
  75.                         lib->lib_IdString     = (APTR)LibId;
  76.  
  77.                         ((struct APIBase *)lib)->Magic = API_MAGIC;
  78.  
  79.                         SegList = segment;
  80.  
  81.                         AddLibrary(lib);
  82.  
  83.                         if (InitC()) {
  84.  
  85.                             return(lib);
  86.                         }
  87.                         else
  88.                             LibExpunge(lib);
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95.     return(NULL);
  96. }
  97.  
  98. /*
  99.  *    Open is given the library pointer and the version request.  Either
  100.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  101.  *    Exec has Forbid() for us during the call.
  102.  */
  103.  
  104. LibCall struct Library *
  105. LibOpen(__D0 long version, __A0 struct Library *lib)
  106. {
  107.     ++lib->lib_OpenCnt;
  108.  
  109.     lib->lib_Flags &= ~LIBF_DELEXP;
  110.  
  111.     return(lib);
  112. }
  113.  
  114. /*
  115.  *    Close is given the library pointer and the version request.  Be sure
  116.  *    not to decrement the open count if already zero.  If the open count
  117.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  118.  *    and return the seglist.  Otherwise we return NULL.
  119.  *
  120.  *    Note that this routine never sets LIBF_DELEXP on its own.
  121.  *
  122.  *    Exec has Forbid() for us during the call.
  123.  */
  124.  
  125. LibCall long
  126. LibClose(__A0 struct Library *lib)
  127. {
  128.     if (lib->lib_OpenCnt && --lib->lib_OpenCnt)
  129.  
  130.         return(NULL);
  131.  
  132.     if (lib->lib_Flags & LIBF_DELEXP)
  133.  
  134.         return(LibExpunge(lib));
  135.  
  136.     return(NULL);
  137. }
  138.  
  139. /*
  140.  *    We expunge the library and return the Seglist ONLY if the open count
  141.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  142.  *    flag and return NULL.
  143.  *
  144.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  145.  *    might be called from the memory allocator and thus we CANNOT DO A
  146.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  147.  *
  148.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  149.  *    therefore freeze if we called it ourselves.  As far as I can tell
  150.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  151.  *    below.
  152.  */
  153.  
  154. LibCall long
  155. LibExpunge(__A0 struct Library *lib)
  156. {
  157.     if (lib->lib_OpenCnt) {
  158.  
  159.         lib->lib_Flags |= LIBF_DELEXP;
  160.  
  161.         return(NULL);
  162.     }
  163.  
  164.     ExitC();
  165.  
  166.     Remove(&lib->lib_Node);
  167.  
  168.     FreeMem((char *)lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize);
  169.  
  170.     if (GadToolsBase) {
  171.  
  172.         CloseLibrary(GadToolsBase);
  173.  
  174.         GadToolsBase = NULL;
  175.     }
  176.  
  177.     if (IntuitionBase) {
  178.  
  179.         CloseLibrary(IntuitionBase);
  180.  
  181.         IntuitionBase = NULL;
  182.     }
  183.  
  184.     if (GfxBase) {
  185.  
  186.         CloseLibrary(GfxBase);
  187.  
  188.         GfxBase = NULL;
  189.     }
  190.  
  191.     if (DOSBase) {
  192.  
  193.         CloseLibrary(DOSBase);
  194.  
  195.         DOSBase = NULL;
  196.     }
  197.  
  198.     return((long)SegList);
  199. }
  200.